// pages/myExchange/myExchange.js
var db = wx.cloud.database()

import {
	areaList
} from '../../miniprogram_npm/@vant/area-data/area-data';
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		areaList,
		nowEditId: '',
		dataList: [],
		show: false,
		flag: false
	},
	showPopup(e) {
		this.setData({
			show: true,
			nowEditId: e.currentTarget.dataset.index
		})
	},
	onClose() {
		this.setData({
			show: false
		})
	},
	queding(e) {
		let area = e.detail.values
		let areaNames = area[0].name + area[1].name + area[2].name;
		this.setData({
			show: false,
			[`dataList[${this.data.nowEditId}].shengshiqu`]: areaNames
		});
	},
	quxiao() {
		this.setData({
			show: false
		})
	},
	restBtn(e) {
		let dataId = e.currentTarget.dataset.id
		db.collection('demouser').doc(dataId).update({
			data: {
				isEdit: true
			}
		}).then(res => {
			this.getData()
		})
	},
	onInput1(e) {
		const dataList = this.data.dataList;
		dataList[e.currentTarget.dataset.index].username = e.detail.value;
		this.setData({
			dataList: dataList
		});
	},
	onInput2(e) {
		const dataList = this.data.dataList;
		dataList[e.currentTarget.dataset.index].usernumber = e.detail.value;
		this.setData({
			dataList: dataList
		});
	},
	onInput3(e) {
		const dataList = this.data.dataList;
		dataList[e.currentTarget.dataset.index].shengshiqu = e.detail.value;
		this.setData({
			dataList: dataList
		});
	},
	onInput4(e) {
		const dataList = this.data.dataList;
		dataList[e.currentTarget.dataset.index].userlocal = e.detail.value;
		this.setData({
			dataList: dataList
		});
	},
	saveBtn(e) {
		let dataId = this.data.dataList[`${e.currentTarget.dataset.index}`]._id
		let datashengshiqu = this.data.dataList[`${e.currentTarget.dataset.index}`].shengshiqu
		let datauserlocal = this.data.dataList[`${e.currentTarget.dataset.index}`].userlocal
		let datausername = this.data.dataList[`${e.currentTarget.dataset.index}`].username
		let datausernumber = this.data.dataList[`${e.currentTarget.dataset.index}`].usernumber
		let reg = /^1[3-9]\d{9}$/;
		console.log(dataId);
		if (!datausername) {
			wx.showToast({
				title: "请输入姓名",
				icon: 'none',
			})
		} else if (!datausernumber) {
			wx.showToast({
				title: "请输入联系电话",
				icon: 'none',
			})
		} else if (!datashengshiqu) {
			wx.showToast({
				title: "请点击选择地址",
				icon: 'none',
			})
		} else if (!datauserlocal) {
			wx.showToast({
				title: "请输入详细地址",
				icon: 'none',
			})
		} else if (!reg.test(datausernumber)) {
			wx.showToast({
				title: "手机格式不对",
				icon: 'none',
			})
		} else {
			db.collection('demouser').doc(dataId).update({
				data: {
					isEdit: false,
					shengshiqu: datashengshiqu,
					userlocal: datauserlocal,
					username: datausername,
					usernumber: datausernumber,
				}
			}).then(res => {
				this.getData()
			})
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	getData() {
		wx.cloud.callFunction({
			name: 'getOpenId', // 调用的云函数名称
			complete: res => {
				db.collection('demouser').where({
					_openid: res.result.openid
				}).get().then(res => {
					if (this.data.flag == false) {
						res.data.forEach(item => {
							item.isEdit = false
						})
					}
					this.setData({
						dataList: res.data,
						flag: true
					})
				})
			}
		})
	},
	onLoad(options) {
		this.getData()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})